/**
 * Created by Administrator on 2020/6/3.
 * Util
 */


let Util={


};


Util.closeBtnClick=function(){

    let u = navigator.userAgent; //识别各种浏览器
    let isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
    let isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
    console.log('closeBanPGame 关闭半屏游戏',isAndroid,isiOS);
    if (isAndroid) {
        window.location = 'guagua://qiqi::24';
    } else if (isiOS) {
        window.location = '#guagua://qiqi::24';
    }


    console.log('closeBtnClick 1 app',web_Appid);
}

Util.gameCallWebRecharge=function(){
    let appid=this.getCookie('oemid');
    console.log('gameCallWebRecharge appid',appid);
    let _url="guagua://qiqi::8::15:1";
    if(appid!=81)_url="#guagua://qiqi::8::15:1";
    window.open(_url, '_self');
}
Util.openGame=function(gameurl){

    let appid=this.getCookie('oemid');
    let _url="guagua://qiqi::33::1";
    if(appid!=81)_url="#"+gameurl;
    console.log('openGame gameurl',gameurl);
    //if(appid!=81)_url="#guagua://qiqi::33::1";
    //let cmdurl=_url+gameurl;
    //console.log('openGame appid',appid);
    window.open(gameurl, '_self');
}

/**获取网页参数**/
Util.GetUrlParam=function(a) {
    var b = new RegExp("(^|&)" + a + "=([^&]*)(&|$)","i")
        , c = window.location.search.substr(1).match(b);
    return null != c ? unescape(c[2]) : "";
}
/**获取cookie**/
Util.getCookie=function(name) {
    let arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
    if(arr=document.cookie.match(reg))
        return unescape(arr[2]);
    else
        return "";
}

/**
 return 端ID(0=PC,1=安卓,2=IOS)
 **/
Util.getAppType=function() {
    //区别端
    let web_Appid=1;//端ID(0=PC,1=安卓,2=IOS)
    if ((navigator.userAgent.match(/(iPhone|iPod|Android|ios|iOS|iPad|Backerry|WebOS|Symbian|Windows Phone|Phone)/i))) {
        console.log("手机访问.");
        web_Appid=1
    }else{
        console.log("电脑访问.");
        web_Appid=0;
    }
    /*if(web_Appid!=0){
        $('.closeBtn').css("display","block");
    }*/
    let web_userAgent= navigator.userAgent; //识别各种浏览器
    let web_isAndroid = web_userAgent.indexOf('Android') > -1 || web_userAgent.indexOf('Adr') > -1; //android终端
    let web_isiOS = !!web_userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
    if (web_isAndroid) {
        web_Appid=1;
    } else if (web_isiOS) {
        web_Appid=2;
    }
    return web_Appid;
}
Util.getDomainName=function() {
    var a = window.location.host.split(".");
    return a[a.length - 2]
}
/**
 * 截取字符串
 * len 截取长度
 */
Util.interceptCharacter=function(str,len){
    if(str==null)str="";
    //trace("CarUtil interceptCharacter",str);
    let i=0;
    let _end=0;
    let _len=0;
    for (i = 0; i < str.length; i++)
    {

        if(str.charCodeAt(i)>10000){
            _len+=2;
        }else{
            _len+=1;
        }
        if(_len<=len){
            _end++;
        }

    }
    if(_end<1)_end=1;
    let _str="";
    for (i = 0; i < _end; i++)
    {
        _str+=str.charAt(i);
    }
    if(_str.length<str.length)_str=_str+"...";
    return _str;
}
/**获取 css类class里的Rotate**/
Util.getCalssRotate=function(elId){
    let el = document.getElementById(elId);
    //console.log(turnId,el);
    let st = window.getComputedStyle(el, null);
    let tr = st.getPropertyValue("-webkit-transform") || st.getPropertyValue("-moz-transform") || st.getPropertyValue("-ms-transform") || st.getPropertyValue("-o-transform") || st.getPropertyValue("transform") || "FAIL";
    //console.log('Matrix: ' + tr);
    let values = tr.split('(')[1].split(')')[0].split(',');
    let a = values[0];
    let b = values[1];
    let c = values[2];
    let d = values[3];
    let scale = Math.sqrt(a * a + b * b);
    //console.log('Scale: ' + scale);
    let sin = b / scale;
    let angle = Math.round(Math.atan2(b, a) * (180 / Math.PI));
    //console.log('Rotate: ' + angle + 'deg');
    return angle;
}
/**加载本土txt,public文件夹下**/
Util.loadFile = function (name) {
    // name为文件所在位置
    let xhr = new XMLHttpRequest()
    const okStatus = document.location.protocol === 'file:' ? 0 : 200
    xhr.open('GET', name, false)
    xhr.overrideMimeType('text/html;charset=utf-8') //默认为utf-8
    xhr.send(null)
    return xhr.status === okStatus ? xhr.responseText : '';
}

/** 获取元素的绝对位置坐标（像对于页面左上角） 单位:px **/
Util.getElementPagePosition=function(element){
    //计算x坐标
    let actualLeft = element.offsetLeft;
    let current = element.offsetParent;
    while (current !== null){
        actualLeft += current.offsetLeft;
        current = current.offsetParent;
    }
    //计算y坐标
    let actualTop = element.offsetTop;
    current = element.offsetParent;
    while (current !== null){
        actualTop += (current.offsetTop+current.clientTop);
        current = current.offsetParent;
    }
    //返回结果
    return {x: actualLeft, y: actualTop}
}

/**返回时间字符串**/
Util.getTimeStr=function(time){
    let _date=new Date(time);
    let _mon=(_date.getMonth()+1);
    if(_mon<10)_mon='0'+_mon;
    let _ri=_date.getDate()+'';
    if(_date.getDate()<10)_ri='0'+_ri;
    let _hours=_date.getHours()+'';
    if(_date.getHours()<10)_hours='0'+_hours;
    let _miu=_date.getMinutes()+'';
    if(_date.getMinutes()<10)_miu="0"+_miu;
    let str=_date.getFullYear()+'-'+_mon+'-'+_ri+' '+_hours+":"+_miu;
    return str;
}
/**返回时间字符串**/
Util.getTimeStr_2=function(time){
    let _date=new Date(time);
    let _mon=(_date.getMonth()+1);
    if(_mon<10)_mon='0'+_mon;
    let _ri=_date.getDate()+'';
    if(_date.getDate()<10)_ri='0'+_ri;
    let _hours=_date.getHours()+'';
    if(_date.getHours()<10)_hours='0'+_hours;
    let _miu=_date.getMinutes()+'';
    if(_date.getMinutes()<10)_miu="0"+_miu;
    let str=_hours+":"+_miu;
    return str;
}


Util.toRecharge=function(){
    let u = navigator.userAgent; //识别各种浏览器
    let isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
    let isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
    console.log('Util toRecharge 充值 123',isAndroid,isiOS);
    if (isAndroid) {
        window.location = 'guagua://qiqi::3::5:145';
    } else if (isiOS) {
        window.location = '#guagua://qiqi::3::5:145';

    }else{
        location.href = "http://ttcmd.qxiu.com/open/chongzhi?agentId=40145&" + Math.random();
        // window.open("http://ttcmd.qxiu.com/open/chongzhi?agentId=40145&" + Math.random());
    }
}
/**
 * 获取现在时间
 */
Util.getDateNow=function(){
    let myDate = new Date();
    let year = myDate.getFullYear(); //获取年
    let month = myDate.getMonth() + 1; //获取月，默认从0开始，所以要加一

    return {
        year: year,
        month: month,
        day: myDate.getDate(),//获取日
        hours: myDate.getHours(),//获取小时
        minutes: myDate.getMinutes(),//获取分
        seconds: myDate.getSeconds()//获取秒
    }
}
/**
 * 获取某月天数
 * @param year
 * @param month 月份加上1，因为月份从0开始计算（0表示一月）
 * @returns {number}
 */
Util.getDaysInMonth=function(year, month) {
    return new Date(year, month + 1, 0).getDate(); // 注意这里要将月份加上1，因为月份从0开始计算（0表示一月）
}
/**
 * 获取某日是星期几
 * @param year
 * @param month
 * @param day
 */
Util.getDateWeek=function(year,month,day){
    let week = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
    let index=new Date(year+'-'+month+'-'+day).getDay();
    return week[index];
}
/**
 * 获取星座
 * @param 月份从0开始计算  0-11
 * @param day
 * @returns {string}
 */
Util.getConstellation=function(month, day) {
    let constellations = ["水瓶座", "双鱼座", "白羊座", "金牛座", "双子座", "巨蟹座", "狮子座", "处女座", "天秤座", "天蝎座", "射手座", "魔羯座"];

    // 将输入的月份转换为数字类型
    month -= 1;

    if (day < 20 || (month === 3 && day <= 20)) {
        return constellations[month];
    } else {
        return constellations[(month + 1) % 12];
    }
}
/**
 * 获取日期的下几天
 * @param year 年
 * @param month 月
 * @param day 日
 * @param num
 * @param type 0:上一天 1:下一天
 */
Util.getDateNextDay=function(year,month,day,num,type){
    let date=new Date(new Date(year+'-'+month+'-'+day).getTime() + (24*60*60*1000)*num);
    if(type==0){
        date=new Date(new Date(year+'-'+month+'-'+day).getTime() - (24*60*60*1000)*num);
    }
    return {
        year: date.getFullYear(),
        month: date.getMonth() + 1,
        day: date.getDate()//获取日
    }
}







export default Util;

